package com.lzw.bluetoothpractice.Adapters;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.lzw.bluetoothpractice.Activitys.DetailForumActivity;
import com.lzw.bluetoothpractice.AppConfig;
import com.lzw.bluetoothpractice.Models.Forum;
import com.lzw.bluetoothpractice.Models.Review;
import com.lzw.bluetoothpractice.Models._User;
import com.lzw.bluetoothpractice.R;
import com.lzw.bluetoothpractice.Utils.GlobalInfo;
import com.lzw.bluetoothpractice.bean.ForumListBean;
import com.lzw.bluetoothpractice.bean.RecommendDoctorBean;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;

public class RecoveryForumListAdapter extends RecyclerView.Adapter<RecoveryForumListAdapter.ViewHolder> {

    private List<Forum> datas = new ArrayList<>();
    View itemView;
    Context mcontext;
    public void updateData(List<Forum> data) {
        if (this.datas == null) {
            this.datas = new ArrayList<>();
        }
        this.datas.clear();
        this.datas.addAll(data);
        notifyDataSetChanged();
    }
    public RecoveryForumListAdapter(Context context) {
        this.mcontext = context;

    }

    class ViewHolder extends RecyclerView.ViewHolder {
        View view;
        TextView tv_question;  //论坛的问题
        TextView tv_review;    //对该问题的回复，一般为点赞数最多的
        TextView tv_username;  //回复者的name
        TextView tv_pointsum;   //该回复的点赞数

        ImageView forum_pic;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView;
            tv_question = (TextView)view.findViewById(R.id.forum_message_question);
            tv_username = (TextView)view.findViewById(R.id.forum_message_username);
            tv_review = (TextView)view.findViewById(R.id.forum_message_review);
            tv_pointsum = (TextView)view.findViewById(R.id.forum_message_pointsum);
            forum_pic = (ImageView)view.findViewById(R.id.forum_pic);
        }
    }
    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Forum forum = datas.get(position);//得到其位置（某一个）
        Long forum_id = forum.getForumId();   //得到这个帖子的id

        holder.tv_question.setText(Html.fromHtml(forum.getForumTitle()));  //获得论坛得帖子


        holder.tv_pointsum.setText(forum.getCommentCnt()+"");//获得展示出来的回复的点赞数
        holder.tv_review.setText(GlobalInfo.delHTMLTag(forum.getForumContent()));  //回复内容
        holder.tv_username.setText(forum.getRemark()+"");  //回复者的名字
        if(!forum.getForumTitle().isEmpty()){
            holder.forum_pic.setVisibility(View.VISIBLE);
            Glide.with(mcontext).load(AppConfig.BASE_URL+forum.getPic()).into(holder.forum_pic);
        }


        //点击事件
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mcontext, DetailForumActivity.class);
                intent.putExtra("forum_id",forum_id+"");
                intent.putExtra("forum_userId",forum.getUserId()+"");
                mcontext.startActivity(intent);
            }
        });
    }
    //得到各个内容
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        itemView = LayoutInflater.from(mcontext).inflate(R.layout.forum_message,parent,false);
        return new ViewHolder(itemView);
    }
}
